/* 
	店铺管理 - shops
 */

/* ------------------------------------------------------------------------------------
	模块导入
------------------------------------------------------------------------------------ */
// 第三方模块 - express
const express = require( 'express' )

/* ------------------------------------------------------------------------------------
	常量、变量、方法
------------------------------------------------------------------------------------ */
// 新建路由
const shopsRouter = express.Router()

/* ------------------------------------------------------------------------------------
	路由配置
------------------------------------------------------------------------------------ */

/* 
|字段|类型|默认值|主键|注释|
|-|-|-|-|-|
|shop_name|varchar(20)|		|PRI|店铺名|
|shop_tel|varchar(11)|		|	|账号|
|shop_product|enum('高汤粉面','经典饭食','小吃炸鸡','奶茶甜品')|经典饭食|		|店铺类型('高汤粉面','经典饭食','小吃炸鸡','奶茶甜品')默认经典饭食|
|shop_floor|enum('桃苑','竹苑','荷苑')|桃苑|		|饭堂位置，竹苑，荷苑，桃苑，默认桃苑|
|hot_number|int(10)|	|		|销量|
|shop_id|int(10) unsigned|		|PRI|店铺主键id|
|avatar_url|varchar(255)|有|		|店铺头像|
|initial_price|int(10) unsigned|5|		|起步价|
|delivery_method|enum('平台配送','商家自送')|平台配送|		|配送方式('平台配送','商家自送')默认平台配送|
|status|enum('营业中','已打烊')|营业中|		|营业状态('营业中','已打烊')|
|del|enum('启用','禁用')|启用|		|启用状态'启用','禁用'|
 */

/* ------------------------------------------------------------------------------------
	POST
------------------------------------------------------------------------------------ */

// 新增店铺
shopsRouter.post( '/add', ( req, res ) => {
	console.log( '添加店铺' )
	// 参数
	const {
		shop_name,
		shop_tel,
	} = req.body

	// 安全阻断
	if ( !shop_id || shop_id === '' || !shop_tel || shop_tel === '' ) return res.sendResult( null, 400,
		'请提供完整信息' )

	res.execSql(
		`INSERT INTO shops(shop_id, shop_tel) VALUES('${shop_id}', '${shop_tel}')`
	)
	res.sendResult( null, 200, '添加成功' )
} )

// 修改店铺信息
shopsRouter.post( '/edit', async ( req, res ) => {
	console.log( '修改店铺信息' )
	// 参数
	const {
		id
	} = req.body

	// 安全阻断
	if ( !id ) return res.sendResult( null, 400, '请提供id' )
	const shop = await res.execSql( `SELECT * FROM shops WHERE shop_id = '${id}'` )
	if ( shop.length <= 0 ) return res.sendResult( null, 400, '无此店铺！请检查id是否正确' )

	// 基础 sql
	let sql = `UPDATE shops SET `

	const filedArr = [
		'shop_name',
		'shop_tel',
		'shop_product',
		'shop_floor',
		'avatar_url',
		'initial_price',
		'delivery_method',
		'status'
	]

	// 遍历加工
	for ( let key in req.body ) {
		if ( filedArr.indexOf( req.body[ key ] !== -1 ) ) sql += `${key} = '${req.body[key]}',`
	}
	sql.substring( 0, sql.length - 1 )
	// 添加条件
	sql += ` WHERE shop_id='${id}'`

	// 发送 sql
	res.execSql( sql )
	res.sendResult( null, 200, `修改成功` )
} )

// 删除店铺
shopsRouter.post( '/delete', async ( req, res ) => {
	console.log( '删除店铺' )
	// 参数
	const {
		id
	} = req.body

	// 安全阻断
	if ( !id ) return res.sendResult( null, 400, '请提供id' )
	const shop = await res.execSql( `SELECT * FROM shop WHERE shop_id = '${id}'` )
	if ( shop.length <= 0 ) return res.sendResult( null, 400, '无此店铺！请检查id是否正确' )

	// 发送 sql
	res.execSql( `UPDATE shop SET del = '禁用' WHERE shop_id = '${id}'` )
	res.sendResult( null, 200, '删除成功' )
} )

/* ------------------------------------------------------------------------------------
	GET
------------------------------------------------------------------------------------ */

// 获取店铺列表数据
shopsRouter.get( '/get', async ( req, res ) => {
	console.log( '获取店铺' )
	// 参数
	const {
		shop_name, //店名
		shop_product, //类型
		delivery_method, //配送方式
		shop_floor //店铺位置
	} = req.query

	const pagenum = req.query.pagenum || 1
	const pagesize = req.query.pagesize || 10

	// 基础 sql 语句
	let sql = `SELECT * FROM shops`

	// 匹配优先级 shop_name > shop_product > delivery_method > shop_floor
	if ( shop_name ) {
		sql += ` WHERE shop_name LIKE '%${shop_name}%'`
	} else if ( shop_product ) {
		sql += ` WHERE shop_product LIKE '%${shop_product}%'`
	} else if ( delivery_method ) {
		sql += ` WHERE delivery_method = '${delivery_method}'`
	} else if ( shop_floor ) {
		sql += ` WHERE shop_floor = '${shop_floor}'`
	}
	// 无查询条件，匹配全部

	const pageindex = ( pagenum - 1 ) * pagesize
	sql += ` LIMIT ${pageindex},${pagesize}`
	// console.log( sql )
	// 发送 sql
	const shops = await res.execSql( sql )

	// 总条数
	const {
		total
	} = ( await res.execSql( `SELECT COUNT(shop_id) AS total FROM shops` ) )[ 0 ]

	// 总页数
	const totalPage = Math.ceil( total / pagesize )

	res.sendResult( {
		shops,
		total,
		pagenum,
		totalPage
	}, 200, '获取成功' )
} )

// 获取热门店铺
shopsRouter.get( '/getHot', async ( req, res ) => {
	console.log( '获取店铺' )

	// 发送 sql
	const shops = await res.execSql( `SELECT * FROM shops WHERE hot_number >= 2000` )

	res.sendResult( {
		shops,
	}, 200, '获取成功' )
} )

// 根据id获取
shopsRouter.get( '/getById', async ( req, res ) => res.getById( 'shops', 'shop_id' ) )

/* ------------------------------------------------------------------------------------
	导出路由
------------------------------------------------------------------------------------ */
module.exports = shopsRouter
